package com.bdqn.vueboot.aop;

import com.bdqn.vueboot.config.BascText;
import com.bdqn.vueboot.config.CustomException;
import com.bdqn.vueboot.config.R;
import com.bdqn.vueboot.entity.Loginlog;
import com.bdqn.vueboot.service.LoginlogService;
import com.bdqn.vueboot.util.RedisUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Aspect
@Component
public class BookingAcpect {
    @Resource
    RedisUtil redisUtil;
    @Resource
    LoginlogService loginlogService;

    private Logger logger = LoggerFactory.getLogger(ValidateTokenAcpect.class);
    @Pointcut("execution(public * com.bdqn.vueboot.controller.BookingController.*(..))")
    public void validateToken() {
    }

    /**
     * 后置通知
     *
     * @param ret
     * @throws Throwable
     */
    @AfterReturning(returning = "ret", pointcut = "validateToken()")
    public void doSuppliesControlleradd(Object ret) throws Throwable {
        // 处理完请求，返回内容
        logger.info("RESPONSE:" + ret);
        R r=(R)ret;
        Loginlog loginlog=new Loginlog();
        loginlog.setLoginLogSof(r.getCode());
        try {
            loginlog.setLoginlogUsrid(BascText.getCarId());
        }catch (Exception e) {
            throw new CustomException("请重新登录！！！");
        }        loginlog.setLoginlogDate(LocalDateTime.now());
        loginlog.setLoginlogDescribe("操作了球场预定！");
        loginlogService.save(loginlog);
    }

}
